package com.redis.demo.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author zqf
 * @Date 2022/10/28 15:33
 * @Description: redis消费队列
 */
@Component
public class MyRedisQueue {


    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Scheduled(cron = "*/5 * * * * ?")
    private void handleQueueMessage(){

        Object queue = redisTemplate.opsForList().rightPop("queue", 5000, TimeUnit.MILLISECONDS);

        if (queue != null) {
            System.out.println("获取到队列消息 " + queue + " 开始执行相关代码逻辑");
        }
    }
}
